<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class DiscountsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addSubscribe($order,$umo_id)
    {
		$data = array(
			'discount' 					=> $order['discount'],
			'user_medicine_order_id' 	=> $umo_id,	
			'admin_paid_status' 	 	=> 0,   
			'added_date'	 			=> date('Y-m-d H:i:s'), 				
			'updated_date'	 			=> date('Y-m-d H:i:s'), 				
			'status'	 				=> 0, 				
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $insertresult;	
    }	
	
	public function userDiscounts($user_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=discounts.user_medicine_order_id'),array('user_id'),'left');
		$select->join('user', new Expression('user.user_id=discounts.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');	
		$select->where('user_medicine_order.user_id="'.$user_id.'"');                                         
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
	}
	public function getShopDiscounts($shop_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=discounts.user_medicine_order_id'),array('user_id'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');	
		$select->where('discounts.shop_id="'.$shop_id.'"');                                         
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
	}
	
}